package com.t.bricks.bussiness.controller.registuser;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.t.bricks.bussiness.controller.common.BaseController;
import com.t.bricks.bussiness.db.entity.registuser.RegistUser;
import com.t.bricks.bussiness.service.common.CommonService;
import com.t.bricks.model.MsgEmity;
import com.t.bricks.utils.modules.web.FindByPageParam;
import com.t.bricks.utils.spring.ModuleUtil;

/**
 * 注册用户控制层
 * 继承BaseController可直接实现通用方法,三个泛型代表:业务类、实体类、查询类
 * 通用业务类CommonService已经包含常用功能,使用自定义RegistUserService(需实现)可覆盖通用方法或扩展业务功能
 * 通用查询类可以直接使用Map,否则使用自定义ConditionRegistUser(需实现)
 * @author HuangXinBian
 * @DateTime 2021年09月07日 13:28:01
 */
@RestController
@Api(value="注册用户", tags={"注册用户操作接口"})
@RequestMapping("/regist/user/")
public class RegistUserController extends BaseController<CommonService, RegistUser, Map<String, Object>> {
	
	/**
	 * 控制操作,首位9不参与用6个数代表开关,0为不限制,1为限制
	 * 6个数的控制分别是:删除、修改、查询、导出、统计、步骤值可逆
	 * 其中前5个为'是否仅创建者可操作'后续为其它控制开关
	 */
	protected int enableTag = 9111111;

	/**
	 * 设置 检查待新增内容是否存在重复数据(多字段组合重复即重复)集合
	 * @return 返回的集合将覆盖父类的checkRepeatCombination成员,若返回null则不进行重复校验
	 */
	@Override
	protected List<String> setCheckRepeatCombination() {
		return Arrays.asList("sName", "sOwner", "sNo");
	}

	/**
	 * 设置 检查待新增内容是否存在重复数据(单独字段重复即重复)集合
	 * @return 返回的集合将覆盖父类的checkRepeatAlone成员,若返回null则不进行重复校验
	 */
	@Override
	protected Map<String, Integer> setCheckRepeatAlone() {
		return null;
	}

	/**
	 * 删除数据
	 * @param iVersion
	 * @param id 表编号
	 * @return MsgEmity
	 */
	@RequestMapping(method={RequestMethod.POST}, value="/del")
	@Override
	public MsgEmity del(Integer iVersion, String id) {
		return super.del(iVersion, id);//直接使用父类中定义的方法,否则使用registUserService.del(iVersion, uId);
	}

	/**
	 * 新增数据
	 * @param registUser 注册用户实体对象
	 * @return MsgEmity
	 */
	@ApiOperation(value="新增数据", response=RegistUser.class)
	@RequestMapping(method={RequestMethod.POST}, value="/add")
	@Override
	public MsgEmity add(@RequestBody RegistUser registUser) {
		HttpServletRequest request = ModuleUtil.currentRequest();
		request.setAttribute("sLoginUserId", "00000000");//将对应的id赋予请求属性,用于后续请求操作 
		
		return super.add(registUser);//直接使用父类中定义的方法,否则使用registUserService.add(registUser);
	}

	/**
	 * 修改数据
	 * @param registUser 注册用户实体对象
	 * @return MsgEmity
	 */
	@ApiOperation(value="修改数据", response=RegistUser.class)
	@RequestMapping(method={RequestMethod.POST}, value="/edit")
	@Override
	public MsgEmity edit(@RequestBody RegistUser registUser) {
		return super.edit(registUser);//直接使用父类中定义的方法,否则使用registUserService.edit(registUser);
	}

	/**
	 * 根据记录编号查询符合分页数据的某条记录
	 * @param id 表编号
	 * @return MsgEmity
	 */
	@ApiOperation(value="根据记录编号查询符合分页数据的某条记录", response=RegistUser.class)
	@RequestMapping(method={RequestMethod.POST}, value="/find/page/row")
	@Override
	public MsgEmity findByPageRow(String id) {
		return super.findByPageRow(id);//直接使用父类中定义的方法,否则使用registUserService.findByPageRow(id);
	}

	/**
	 * 查询分页数据
	 * @param findByPageParam 分页查询条件组合类
	 * 结构参考: {"page": 分页请求信息对象, "orders": 排序信息对象集合, "sLikeStr": 全文检索, "condition": 查询条件对象}
	 * @return MsgEmity 返回查询结果
	 */
	@ApiOperation(value="查询分页数据", response=RegistUser.class)
	@RequestMapping(method={RequestMethod.POST}, value="/find/page")
	@Override
	public MsgEmity findByPage(@RequestBody FindByPageParam<Map<String, Object>> findByPageParam) {//findByPage(@RequestBody FindByPageParam<ConditionRegistUser> findByPageParam)
		return super.findByPage(findByPageParam);//直接使用父类中定义的方法,否则使用 registUserService.findByPage(findByPageParam.getPage(), findByPageParam.getOrders(), findByPageParam.getsLikeStr(), findByPageParam.getCondition());
	}

	//---------------------------- 自动生成代码结束 ----------------------------//

	/**
	 * 判断用户是否已经注册
	 * @param sName
	 * @param sNo
	 * @param sOwner
	 * @return
	 */
	@ApiOperation(value="判断用户是否已经注册")
	@ApiImplicitParams({
		@ApiImplicitParam(name="sName", value="用户名", dataType="String", paramType="query", required=true),
		@ApiImplicitParam(name="sNo", value="电话号", dataType="String", paramType="query", required=true),
		@ApiImplicitParam(name="sOwner", value="来源", dataType="String", paramType="query", required=true)
	})
	@RequestMapping(method={RequestMethod.POST}, value="/has")
	public MsgEmity hasUser(String sName, String sNo, String sOwner) {
		RegistUser registUser = new RegistUser();
		registUser.setsName(sName);
		registUser.setsNo(sNo);
		registUser.setsOwner(sOwner);
		
		return this.getBaseService().findCountByKey(registUser, false);
	}

	/**
	 * 拉取用户信息
	 * @param registUser
	 * @return
	 */
	@ApiOperation(value="拉取用户信息", notes="参数中至少提供:用户名、电话号、密码、来源")
	//[start]
	@ApiResponses({
		@ApiResponse(code=200, message="{\"success\": 布尔值, \"msg\": 文字提示, \"data\": 码值}"),
		@ApiResponse(code=400, message="错误的请求,请检查路径"),
		@ApiResponse(code=404, message="请求路径没有或页面跳转路径不对"),
		@ApiResponse(code=1000001, message="内部请求密钥错误"),
		@ApiResponse(code=1000002, message="您还没有登录或登录已超时,需要重新登录"),
		@ApiResponse(code=1000003, message="检查登录状态失败,请稍后重试！"),
		@ApiResponse(code=1000004, message="请求的操作不存在,请检查访问地址正确性，如大小写"),
		@ApiResponse(code=1000005, message="响应后在系统内部发生不可预知的错误"),
		@ApiResponse(code=1019000, message="1019000~1999999为拦截器调用方法错误"),
		@ApiResponse(code=8001, message="参数为空"),
		@ApiResponse(code=8002, message="用户名为空"),
		@ApiResponse(code=8003, message="电话号为空"),
		@ApiResponse(code=8004, message="密码为空"),
		@ApiResponse(code=8005, message="来源为空"),
		@ApiResponse(code=7001, message="数据不存在"),
		@ApiResponse(code=7002, message="查询发生异常"),
		@ApiResponse(code=7003, message="7003~8998为查询时发生的其它错误")
	})
	//[end]
	@RequestMapping(method={RequestMethod.POST}, value="/pull")
	public MsgEmity pullUser(@RequestBody RegistUser registUser) {
		String[] valids = {"sName", "sNo", "sPass", "sOwner"};
		return this.getBaseService().findOneByKey(registUser, valids, false);
	}

	/**
	 * 找回用户信息
	 * @param registUser
	 * @return
	 */
	@ApiOperation(value="找回用户信息", notes="参数中至少提供:用户名、电话号、问题、答案、来源")
	//[start]
	@ApiResponses({
		@ApiResponse(code=200, message="{\"success\": 布尔值, \"msg\": 文字提示, \"data\": 码值}"),
		@ApiResponse(code=400, message="错误的请求,请检查路径"),
		@ApiResponse(code=404, message="请求路径没有或页面跳转路径不对"),
		@ApiResponse(code=1000001, message="内部请求密钥错误"),
		@ApiResponse(code=1000002, message="您还没有登录或登录已超时,需要重新登录"),
		@ApiResponse(code=1000003, message="检查登录状态失败,请稍后重试！"),
		@ApiResponse(code=1000004, message="请求的操作不存在,请检查访问地址正确性，如大小写"),
		@ApiResponse(code=1000005, message="响应后在系统内部发生不可预知的错误"),
		@ApiResponse(code=1019000, message="1019000~1999999为拦截器调用方法错误"),
		@ApiResponse(code=8001, message="参数为空"),
		@ApiResponse(code=8002, message="用户名为空"),
		@ApiResponse(code=8003, message="电话号为空"),
		@ApiResponse(code=8004, message="密码为空"),
		@ApiResponse(code=8005, message="来源为空"),
		@ApiResponse(code=7001, message="数据不存在"),
		@ApiResponse(code=7002, message="查询发生异常"),
		@ApiResponse(code=7003, message="7003~8998为查询时发生的其它错误")
	})
	//[end]
	@RequestMapping(method={RequestMethod.POST}, value="/pull")
	public MsgEmity retrieveUser(@RequestBody RegistUser registUser) {
		RegistUser registUserData = new RegistUser();
		registUserData.setsName(registUser.getsName());
		registUserData.setsNo(registUser.getsNo());
		registUserData.setsOwner(registUser.getsOwner());
		registUserData.setsQuestion1(registUser.getsQuestion1());
		registUserData.setsAnswer1(registUser.getsAnswer1());
		
		String[] valids = {"sName", "sNo", "sQuestion1", "sAnswer1", "sOwner"};
		MsgEmity me = this.getBaseService().findOneByKey(registUser, valids, false);
		if (me.isSuccess()) {
			return me;//找到
		}
		
		registUserData.setsName(registUser.getsName());
		registUserData.setsNo(registUser.getsNo());
		registUserData.setsOwner(registUser.getsOwner());
		registUserData.setsQuestion1(null);
		registUserData.setsAnswer1(null);
		registUserData.setsQuestion2(registUser.getsQuestion1());
		registUserData.setsAnswer2(registUser.getsAnswer1());

		valids = new String[] {"sName", "sNo", "sQuestion2", "sAnswer2", "sOwner"};
		return this.getBaseService().findOneByKey(registUser, valids, false);
	}
	
}
